#include <sstream>
#include <vector>

#include "RedisClient.h"
#include "Logic.h"
#include "json.h"
#include "util.h"
#include "DbPart.h"
#include "Game.h"


CDbPart::CDbPart()
{
	m_configRedis = nullptr;
}

CDbPart::~CDbPart()
{
}

bool CDbPart::CheckServerReady()
{
	return m_configRedis != nullptr;
}

bool CDbPart::LoadTgBotCfg(const std::string& phpKey)
{
	assert(m_configRedis != NULL);
	
	if (phpKey != TG_BOT_CONFIG) {
		log_error("Invalid phpKey: %s", phpKey.c_str());
		return false;
	}

	tgBotCfg_Map.clear();

	int ret = m_configRedis->Hgetall(phpKey, &tgBotCfg_Map);
    if (ret < 0 || tgBotCfg_Map.empty()) {
        log_error("Redis Hgetall Failed, php_key: %s, ret: %d;", phpKey.c_str(), ret);
        return false;
    }

	log_debug("Redis Hgetall Success, key: %s, config_map size: %zu;", phpKey.c_str(), tgBotCfg_Map.size());
	return true;
}

void CDbPart::UpdatePhpCfg(const std::string& name)
{
	if(0 == name.compare(TG_BOT_CONFIG))
	{
		tgBotCfg_Map.clear();
		if(!LoadTgBotCfg(TG_BOT_CONFIG)) {
			log_error("Tg Bot Config update Failed, php_key: %s;", name.c_str());
		}
		else
		{
			log_debug("Tg Bot Config update Success !!!");
		}
	}
	
}
	
	

const std::map<std::string, std::string>& CDbPart::GetTgBotCfgMap() const
{
	return tgBotCfg_Map;
}




